<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-5-11
 * Time: 9:11
 */

namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\common\model\Goods as Goods_Model;
use app\common\model\GoodsCategory;
use think\Db;

class Goods extends Admin
{

    //公告列表
    public function index(){
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = Goods_Model::alias('g')
            ->leftJoin('dp_qt_goods_category c','c.id=g.goods_category_id')
            ->field('g.*,c.name category_name')
            ->where($map)->order($order)->paginate();
        // 分页数据
        $page = $data_list->render();

        $btn_edit = [
            'title' => '修改商品',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-info btn-sm',
            'href' => url('edit', ['id' => '__id__']),
        ];
        //设置页面
        return ZBuilder::make('table')
            ->addColumn('id', '编号', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('name', '商品名称', 'text')
            ->addColumn('category_name', '分类', 'text')
            ->addColumn('price', '价格', 'text')
            ->addColumn('stock', '库存', 'text')
            ->addColumn('img', '商品图', 'image')
            ->addColumn('is_sale', '在售', 'status', '', [1 => '是:success', 0 => '否:danger'])
            ->addColumn('create_time', '发布日期', 'datetime', '', 'Y-m-d H:i:s')
            ->addColumn('update_time', '编辑日期', 'datetime', '', 'Y-m-d H:i:s')
            ->addColumn('right_button', '操作', 'btn')
            ->setTableName('qt_goods')
            ->addTopButton('add')// 添加顶部按钮
            ->addTopButton('delete')// 添加顶部按钮
            ->addRightButton('edit', $btn_edit)// 添加顶部按钮
            ->setPrimaryKey('id')// 设置主键名为username
            ->setPageTitle('商品列表')
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch();
    }

    //添加广告位置
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'Goods');
            if ($page = Goods_Model::create($data)) {
                $this->success('新增成功', url('index',['']));
            } else {
                $this->error('新增失败');
            }
        } else {
            $list = GoodsCategory::column('id,name') ;
            return ZBuilder::make('form')
                ->addText('sort','请输入排序')
                ->addText('name','请输入商品名称')
                ->addText('price','请输入商品价格:')
                ->addText('stock','请输入商品数量:')
                ->addText('unit','请输入商品单位:')
                ->addText('description','请输入商品描述:')
                ->addSelect('goods_category_id','请选择分类','',$list)
                ->addText('engineer_rate','请输入技师提成:')
                ->addText('receiver_rate','请输入接待提成:')
                ->addImage('img','请上传商品图')
                ->addRadio('is_digit', '小数计量', '', [1 => '是', 0 => '否'])
                ->addRadio('is_change_price', '修改会员价格', '', [1 => '是', 0 => '否'])
                ->addRadio('is_viper_pay', '会员支付', '', [1 => '是', 0 => '否'])
                ->addRadio('is_sale', '在售', '', [1 => '是', 0 => '否'])
                ->setPageTitle('编辑商品')
                ->fetch();
        }
    }


    //编辑公告
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Goods');
            if (true !== $result) $this->error($result);

            if (Goods_Model::where('id',$id)->update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = Goods_Model::get($id);
            $list = GoodsCategory::column('id,name') ;
            return ZBuilder::make('form')
                ->addText('sort','请输入排序')
                ->addText('name','请输入商品名称')
                ->addText('price','请输入商品价格:')
                ->addText('stock','请输入商品数量:')
                ->addText('unit','请输入商品单位:')
                ->addText('description','请输入商品描述:')
                ->addSelect('goods_category_id','请选择分类','',$list)
                ->addText('engineer_rate','请输入技师提成:')
                ->addText('receiver_rate','请输入接待提成:')
                ->addImage('img','请上传商品图')
                ->addRadio('is_digit', '小数计量', '', [1 => '是', 0 => '否'])
                ->addRadio('is_change_price', '修改会员价格', '', [1 => '是', 0 => '否'])
                ->addRadio('is_viper_pay', '会员支付', '', [1 => '是', 0 => '否'])
                ->addRadio('is_sale', '在售', '', [1 => '是', 0 => '否'])
                ->setFormData($info)
                ->setPageTitle('编辑商品')
                ->fetch();
        }
    }

    //删除公告
    public function delete($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        $re = Goods_Model::destroy($id);
        if ($re) {
            $this->success('删除成功', 'index');
        } else {
            $this->error('系统繁忙请稍后再试', 'index');
        }

    }
}